package com.cjb.common.util;

import javax.crypto.Cipher;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;

/**
 * @Author chenjinbin
 * @Package IntelliJ IDEA
 * @Class Test
 * @Date 2024-03-02 下午5:02
 */
public class Test {
    public static void main(String[] args) throws Exception {
        // 创建并初始化DigitalCertificate对象
        DigitalCertificate digitalCert = new DigitalCertificate("-----BEGIN CERTIFICATE-----\n" +
                "MIICKjCCAdCgAwIBAgIUHC5YPWXprEOakdh7M5NyF8q9iI4wCgYIKoZIzj0EAwIw\n" +
                        "cDELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMQ8wDQYDVQQH\n" +
                        "EwZEdXJoYW0xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHDAaBgNVBAMTE2Nh\n" +
                        "Lm9yZzEuZXhhbXBsZS5jb20wHhcNMjQwMzAyMDY0NTAwWhcNMjUwMzAyMDY1MDAw\n" +
                        "WjAhMQ8wDQYDVQQLEwZjbGllbnQxDjAMBgNVBAMTBWFkbWluMFkwEwYHKoZIzj0C\n" +
                        "AQYIKoZIzj0DAQcDQgAEjCKneBCo5SkpUnh1s9x4y311iFrQTuv04HbEUIa00dg+\n" +
                        "9+CB6L1EpOeJc6jBb6RFVX5Z91gfsuk3u8rn7DHHJ6OBljCBkzAOBgNVHQ8BAf8E\n" +
                        "BAMCA6gwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMAwGA1UdEwEB/wQC\n" +
                        "MAAwHQYDVR0OBBYEFPimCpIH1rARa2dGHJVTPe9yD3j6MB8GA1UdIwQYMBaAFMFQ\n" +
                        "7te/enBZD7y4jabYXlO3XIENMBQGA1UdEQQNMAuCCWxvY2FsaG9zdDAKBggqhkjO\n" +
                        "PQQDAgNIADBFAiEAi7Lwt09qK0Qazl4Y5wd9MdQ+DCr+SEqR3RY58DDpUPQCIFk2\n" +
                        "9gWB49KsPWhBS//md1iE+xG0WllfJbC2B2URXlHT\n" +
                        "-----END CERTIFICATE-----","-----BEGIN PRIVATE KEY-----\n" +
                "MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgOuM/TkJbT3lVEZ7m\n" +
                        "D+V0x/A+7FIH6jtYhcpGWCu4yK2gCgYIKoZIzj0DAQehRANCAASMIqd4EKjlKSlS\n" +
                        "eHWz3HjLfXWIWtBO6/TgdsRQhrTR2D734IHovUSk54lzqMFvpEVVfln3WB+y6Te7\n" +
                        "yufsMccn\n" +
                        "-----END PRIVATE KEY-----");

        // 从证书中提取公钥
        PublicKey publicKey = digitalCert.getCertificate().getPublicKey();

        // 使用私钥进行解密
        PrivateKey privateKey = digitalCert.getPrivateKey();

        System.out.println("打印公钥"+publicKey);
        System.out.println("打印私钥"+privateKey);
        // 假设我们有一些要加密的数据
        String originalData = "数字证书加密";

        // 加密过程
        Cipher ecEncryptCipher = Cipher.getInstance("ECIES"); // 或者其他的ECC加密算法名
        ecEncryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedBytes = ecEncryptCipher.doFinal(originalData.getBytes());
        System.out.println("打印密文"+encryptedBytes);

        // 解密过程
        // 对于ECC解密
        Cipher ecDecryptCipher = Cipher.getInstance("ECIES"); // 同样的，确保支持ECDH或者ECIES解密
        ecDecryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedBytes = ecDecryptCipher.doFinal(encryptedBytes);

        // 转换回字符串并打印
        String decryptedData = new String(decryptedBytes);
        System.out.println("Decrypted data: " + decryptedData);
    }
}
